package com.tgy.dao;

import com.tgy.entity.Department;
import com.tgy.entity.Employee;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/***
 * @ClassName: Employee
 * @Description:
 * @Auther: 送一一块二向箔
 * @Date: 11:38 2021/12/27
 * @version : V1.0
 */
@Repository
public class EmployeeDao {
    private static Map<Integer, Employee> employees = null;
    @Resource
    private DepartmentDao departmentDao;
    static {

        employees = new HashMap<>();
        employees.put(101,new Employee(101,"aaaa","14545314@qq.com",1,new Department(101,"aaaa")));
        employees.put(102,new Employee(102,"bbbb","143543414@qq.com",0,new Department(102,"bbbb")));
        employees.put(103,new Employee(103,"cccc","14453314@qq.com",1,new Department(103,"cccc")));
        employees.put(104,new Employee(104,"dddd","14453314@qq.com",0,new Department(104,"dddd")));
        employees.put(105,new Employee(105,"eeee","14543314@qq.com",1,new Department(105,"eeee")));
    }

    //主键自增
    private static Integer initId = 1006;
    //增加员工
    public void save(Employee employee){
        if (employee.getId() == null){
            employee.setId(initId++);
        }
        employee.setDepartment(departmentDao.getById(employee.getDepartment().getId()));
        employees.put(employee.getId(),employee);
    }

    //查询全部
    public Collection<Employee> getAll(){
        return employees.values();
    }
    //通过id查询
    public Employee getEmployeeById(Integer id){
        return employees.get(id);
    }
    //删除员工
    public void delete(Integer id) {
         employees.remove(id);
    }
}
